install.packages("PanelMatch")
library(PanelMatch)
rm(list=ls())

## Set working directory to the folder that includes the dataset
# setwd("/Users/jamesdkim/Library/CloudStorage/Dropbox/Working Papers/Leader War Exposure and Conflict Behavior/ISQ R&R/Replication-ISQ")
## Import the dataset
# leader_warexp.simple <- read.csv("~/Library/CloudStorage/Dropbox/Working Papers/Leader War Exposure and Conflict Behavior/ISQ R&R/Replication-ISQ/leader_warexp-simple.csv")


# get a matched set without refinement
sets0 <- PanelMatch(lag = 2, time.id = "year", unit.id = "ccode",
                    treatment = "cwtrauma", refinement.method = "none",
                    data = leader_warexp.simple, match.missing = FALSE,
                    covs.formula = ~ combat + rebel + milnoncombat + age + gender + ln_cinc + major_ally + status_dissat_l5 +polity2 + I(lag(midonset, 1:5)),
                    size.match = 3, qoi = "att",
                    outcome.var = "midonset",
                    lead = 0:4, forbid.treatment.reversal = FALSE)


# get a matched set with refinement using CBPS.match, setting the
# size of matched set to 5
sets1 <- PanelMatch(lag = 2, time.id = "year", unit.id = "ccode",
                    treatment = "cwtrauma", refinement.method = "mahalanobis",
                    data = leader_warexp.simple, match.missing = FALSE,
                    covs.formula = ~ combat + rebel + milnoncombat + age + gender + ln_cinc + major_ally + status_dissat_l5 +polity2 + I(lag(midonset, 1:5)),
                    size.match = 3, qoi = "att",
                    outcome.var = "midonset",
                    lead = 0:4, forbid.treatment.reversal = FALSE)

# get another matched set with refinement using CBPS.weight
sets2 <- PanelMatch(lag = 2, time.id = "year", unit.id = "ccode",
                    treatment = "cwtrauma", refinement.method = "ps.weight",
                    data = leader_warexp.simple, match.missing = FALSE,
                    covs.formula = ~ combat + rebel + milnoncombat + age + gender + ln_cinc + major_ally + status_dissat_l5 +polity2 + I(lag(midonset, 1:5)),
                    size.match = 3, qoi = "att",
                    outcome.var = "midonset",
                    lead = 0:4, forbid.treatment.reversal = FALSE)


# Appendix Figure 3: Covariate balance
balance_scatter(non_refined_set = sets0$att,
                matched_set_list = list(sets1$att, sets2$att),
                data = leader_warexp.simple,
                covariates = c("midonset", "ln_cinc", "polity2", "age", "gender", "combat", "rebel", "milnoncombat", "major_ally", "status_dissat_l5"))
# add legend
legend(x = 0, y = 0.8,
       legend = c("mahalanobis",
                  "PS weighting"),
       y.intersp = 0.65,
       x.intersp = 0.3,
       xjust = 0,
       pch = c(1, 3), pt.cex = 1,
       bty = "n", ncol = 1, cex = 1, bg = "white")


# Appendix Figure 4: Treatment effects over time
#not including any lagged variables
PM.results <- PanelMatch(lag = 2, time.id = "year", unit.id = "ccode",
                         treatment = "cwtrauma", refinement.method = "mahalanobis",
                         data = leader_warexp.simple, match.missing = TRUE,
                         covs.formula = ~ combat + rebel + milnoncombat + age + gender + ln_cinc + major_ally +polity2 + I(lag(midonset, 1:5)) + status_dissat_l5,
                         size.match = 3, qoi = "att",
                         outcome.var = "midonset", lead = 0:9, forbid.treatment.reversal = FALSE)

PE.results <- PanelEstimate(sets = PM.results, data = leader_warexp.simple)
plot(PE.results)